package org.jeecg.modules.wlhy.hytaxperincomedet.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.third.dongjiang.tax.system.dto.ApiBaseResDTO;
import org.jeecg.modules.third.dongjiang.tax.system.dto.req.perincomedet.PerincomedetDTO;
import org.jeecg.modules.third.dongjiang.tax.system.service.DongjiangTaxSystemService;
import org.jeecg.modules.wlhy.hytaxperincomedet.entity.HyTaxPerincomedet;
import org.jeecg.modules.wlhy.hytaxperincomedet.mapper.HyTaxPerincomedetMapper;
import org.jeecg.modules.wlhy.hytaxperincomedet.service.IHyTaxPerincomedetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 个人所得税汇总信息
 * @Author: duanlikao
 * @Date:   2021-03-19
 * @Version: V1.0
 */
@Service
public class HyTaxPerincomedetServiceImpl extends ServiceImpl<HyTaxPerincomedetMapper, HyTaxPerincomedet> implements IHyTaxPerincomedetService {

    @Autowired
    private DongjiangTaxSystemService dongjiangTaxSystemService;

    @Resource
    private HyTaxPerincomedetMapper taxPerincomedetMapper;

    @Override
    public ApiBaseResDTO uploadTax(HyTaxPerincomedet hyTaxPerincomedet) {

        HyTaxPerincomedet hyTaxPerincomedetDb = getById(hyTaxPerincomedet.getId());
        if(hyTaxPerincomedetDb == null) {
            throw new JeecgBootException("未找到对应数据");
        }
        //if(hyTaxPerincomedetDb.getUploadStatus().equals("1")){
        //    throw new JeecgBootException("该条数据已经上报："+hyTaxPerincomedet.getId());
        //}
        PerincomedetDTO perincomedetDTO = HyTaxPerincomedetTOPerincomedetDTO(hyTaxPerincomedetDb);
        if(perincomedetDTO == null){
            throw new JeecgBootException("未找到对应数据");
        }

        ApiBaseResDTO reportResult = dongjiangTaxSystemService.sendPerincomedet(perincomedetDTO);

        Integer code =  StringUtils.isNotEmpty(reportResult.getCode())? Integer.valueOf(reportResult.getCode()) : 0;

        hyTaxPerincomedetDb.setUploadStatus(code >= 100 && code < 200  ? "1" :"2");
        hyTaxPerincomedetDb.setUploadDate(new Date());
        hyTaxPerincomedetDb.setSbrq(new Date());
        updateById(hyTaxPerincomedetDb);
        return reportResult;
    }

    /**
     * 统计已经个税累计已缴税额
     * @param startMonth
     * @param endMonth
     * @return
     */
    @Override
    public Double getTaxPerincomedetByMonth(String startMonth, String endMonth) {
        return taxPerincomedetMapper.getTaxPerincomedetByMonth( startMonth,  endMonth);
    }



    public static PerincomedetDTO HyTaxPerincomedetTOPerincomedetDTO(HyTaxPerincomedet hyTaxPerincomedet) {
        if (hyTaxPerincomedet == null) {
            return null;
        }
        PerincomedetDTO perincomedetDTO = new PerincomedetDTO();
        perincomedetDTO.setSkssqq(
                DateUtils.date2Str( hyTaxPerincomedet.getSkssqq() ,DateUtils.date_sdf.get())
        );
        perincomedetDTO.setSkssqz(
                DateUtils.date2Str( hyTaxPerincomedet.getSkssqz(),DateUtils.date_sdf.get())
        );
        perincomedetDTO.setSbrq(
                DateUtils.date2Str(new Date(),DateUtils.date_sdf.get())
        );
        perincomedetDTO.setNsrsbh(hyTaxPerincomedet.getNsrsbh());
        perincomedetDTO.setNsrmc(hyTaxPerincomedet.getNsrmc());
        perincomedetDTO.setSbrs(hyTaxPerincomedet.getSbrs());
        perincomedetDTO.setSl(String.format("%.2f",hyTaxPerincomedet.getSl()));
        perincomedetDTO.setYssr(String.format("%.2f",hyTaxPerincomedet.getYssr()));
        perincomedetDTO.setYnse(String.format("%.2f",hyTaxPerincomedet.getYnse()));
        perincomedetDTO.setLjyjnse(
                hyTaxPerincomedet.getLjyjnse() != null ?   String.format("%.2f",hyTaxPerincomedet.getLjyjnse()):
                        String.valueOf(00.00)
        );
        perincomedetDTO.setBqybtse(String.format("%.2f",hyTaxPerincomedet.getBqybtse()));
        return perincomedetDTO;
    }

}
